﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace frmCheckStock
{
    public partial class frmStockCard : Form
    {
        LogicUniversityT2Entities logic = new LogicUniversityT2Entities();

        public frmStockCard()
        {
            InitializeComponent();
        }

        private void frmStockCard_Load(object sender, EventArgs e)
        {
            var categoryData = logic.Categories;
            cboCategoryName.DataSource = categoryData;
            cboCategoryName.DisplayMember = "CategoryName";

            var itemData = logic.Items;
            cboItemDescription.DataSource = itemData;
            cboItemDescription.DisplayMember = "ItemDescription";

            lblItemID.Text = "";
            lblUOM.Text = "";
            lblHighSupplier.Text = "";
            lblMediumSupplier.Text = "";
            lblLowSupplier.Text = "";
            lblStockBalance.Text = "";
        }

        private void cboCategoryName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Category c = (Category)cboCategoryName.SelectedItem;

            var itemList = from item in logic.Items
                           where item.CategoryID == c.CategoryID
                           select item.ItemDescription;
            var itemlist = itemList.ToList();
            cboItemDescription.DataSource = itemlist;
            cboItemDescription.DisplayMember = itemlist.ToString();
        
        }

        private void cboItemDescription_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a =null;
             Item c = new Item();
            if (cboItemDescription.SelectedValue.ToString() != "frmCheckStock.Item")
            {
                a = cboItemDescription.SelectedValue.ToString();

                c = logic.Items.FirstOrDefault<Item>(d => d.ItemDescription == a);

                Supplier s = logic.Suppliers.FirstOrDefault<Supplier>(d => d.SupplierID == c.FirstSupplier);
                Supplier s1 = logic.Suppliers.FirstOrDefault<Supplier>(d => d.SupplierID == c.SecondSupplier);
                Supplier s2 = logic.Suppliers.FirstOrDefault<Supplier>(d => d.SupplierID == c.ThirdSupplier);
                
                lblItemID.Text = c.ItemID;
                lblUOM.Text = c.UOM;
                lblHighSupplier.Text = s.SupplierName;
                lblMediumSupplier.Text = s1.SupplierName;
                lblLowSupplier.Text = s2.SupplierName;
                lblStockBalance.Text = c.StockBalance.ToString();
            }
                var deptReqList = from deptRequisition in logic.DepartmentRequisitions
                                  join department in logic.Departments
                                  on deptRequisition.DepartmentID equals department.DepartmentID
                                  where deptRequisition.ItemID == c.ItemID
                                  select new
                                  {
                                      date = deptRequisition.DeliveredDate,
                                      dept_supplier = department.DepartmentName,
                                      quantity = deptRequisition.DeliveredQuantity,
                                  };
                var deptReqlist = deptReqList.ToList();

                var purchDetailList = from purchDetail in logic.PurchaseDetails
                                      join purchaseOrder in logic.PurchaseOrders
                                      on purchDetail.PurchaseOrderID equals purchaseOrder.PurchaseOrderID
                                      join supplier in logic.Suppliers
                                      on purchaseOrder.SupplierID equals supplier.SupplierID
                                      where purchDetail.ItemID == c.ItemID
                                      select new
                                      {
                                          date = purchDetail.Date,
                                          dept_supplier = supplier.SupplierName,
                                          quantity = purchDetail.ReceivedQuantity
                                      };
                var purchDetaillist = purchDetailList.ToList();

                var adjVoucherList = from adjustmentVoucher in logic.AdjustmentVouchers
                                     where adjustmentVoucher.ItemID == c.ItemID
                                     select new
                                      {
                                          date = adjustmentVoucher.Date,
                                          dept_supplier = "Stock Adjustment",
                                          quantity = adjustmentVoucher.Quantity
                                      };
                var adjVoucherlist = adjVoucherList.ToList();
                var result = deptReqlist
                .Concat(purchDetaillist)
                .Concat(adjVoucherlist)
                .OrderBy(d=>d.date)
                .ToList();
                gvStockCard.DataSource = result;
        }
    }
}


